package com.dd.admin.business.memberCard.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dd.admin.business.member.domain.MemberVo;
import com.dd.admin.business.member.entity.Member;
import com.dd.admin.business.member.service.MemberService;
import com.dd.admin.common.security.SecurityUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.dd.admin.common.model.UpdateGroup;
import com.dd.admin.common.model.result.ResultBean;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotBlank;
import com.dd.admin.business.memberCard.entity.MemberCard;
import com.dd.admin.business.memberCard.domain.MemberCardVo;
import com.dd.admin.business.memberCard.domain.MemberCardDto;
import com.dd.admin.business.memberCard.service.MemberCardService;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 会员卡 前端控制器
 * </p>
 *
 * @author 727869402@qq.com
 * @since 2024-06-19
 */
@Api(tags = "会员卡")
@RestController
public class MemberCardController {

    @Autowired
    MemberCardService memberCardService;

    @Autowired
    MemberService memberService;

    @ApiOperation(value = "会员卡-分页列表")
    @ApiOperationSupport(order = 1)
    @GetMapping("/admin/memberCard/page")
    public ResultBean<IPage<MemberCardVo>> page(MemberCardDto memberCardDto) {
        memberCardDto.setShopId(SecurityUtil.getLoginUser().getDeptId());
        IPage<MemberCardVo> pageInfo = memberCardService.selectMemberCardPage(memberCardDto);
        return ResultBean.success(pageInfo);
    }

    @ApiOperation(value = "会员卡-列表")
    @ApiOperationSupport(order = 2)
    @GetMapping("/admin/memberCard/list")
    public ResultBean<List<MemberCardVo>> list(MemberCardDto memberCardDto) {
        memberCardDto.setShopId(SecurityUtil.getLoginUser().getDeptId());
        List<MemberCardVo> list = memberCardService.selectMemberCardList(memberCardDto);
        return ResultBean.success(list);
    }

    @ApiOperation(value = "会员卡-列表")
    @ApiOperationSupport(order = 2)
    @GetMapping("/admin/memberCard/searchList")
    public ResultBean<List<MemberCardVo>> searchList(MemberCardDto memberCardDto) {
        memberCardDto.setShopId(SecurityUtil.getLoginUser().getDeptId());
        memberCardDto.setLimit(10);
        List<MemberCardVo> list = memberCardService.selectMemberCardList(memberCardDto);
        list.stream().forEach(memberCardVo -> {
            Member member = memberService.getById(memberCardVo.getMemberId());
            MemberVo memberVo = BeanUtil.copyProperties(member, MemberVo.class);
            memberCardVo.setMember(memberVo);
        });
        return ResultBean.success(list);
    }

    @ApiOperation(value = "会员卡-添加")
    @ApiOperationSupport(order = 3)
    @PostMapping("/admin/memberCard/add")
    public ResultBean<MemberCard> add(@RequestBody @Validated MemberCardDto memberCardDto) {
        MemberCard memberCard = BeanUtil.copyProperties(memberCardDto, MemberCard.class);
        memberCardService.save(memberCard);
        return ResultBean.success(memberCard);
    }

    @ApiOperation(value = "会员卡-查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/admin/memberCard/{memberCardId}")
    public ResultBean<MemberCardVo> get(@PathVariable @NotBlank String memberCardId) {
        MemberCard memberCard = memberCardService.getById(memberCardId);
        MemberCardVo memberCardVo = BeanUtil.copyProperties(memberCard,MemberCardVo.class);
        return ResultBean.success(memberCardVo);
    }

    @ApiOperation(value = "会员卡-修改")
    @ApiOperationSupport(order = 5)
    @PostMapping("/admin/memberCard/update")
    public ResultBean<MemberCard> update(@RequestBody @Validated(UpdateGroup.class) MemberCardDto memberCardDto) {
        MemberCard memberCard = BeanUtil.copyProperties(memberCardDto, MemberCard.class);
        memberCardService.updateById(memberCard);
        return ResultBean.success(memberCard);
    }

    @ApiOperation(value = "会员卡-删除")
    @ApiOperationSupport(order = 6)
    @GetMapping("/admin/memberCard/delete/{memberCardId}")
    public ResultBean<MemberCard> delete(@PathVariable @NotBlank String memberCardId) {
        Boolean b = memberCardService.removeById(memberCardId);
        return ResultBean.success(b);
    }
}
